package net.trustie.model;

import java.util.List;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;

import org.apache.commons.codec.digest.DigestUtils;

import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

public class cto51_blog_Model implements AfterExtractor, ValidateExtractor {

	private int post_id = 0;
	private String post_url = "";
	@ExtractBy("//*div[@class='showTitleBOx']/div[@class='showTitle']/allText()")
	private String post_title = "";
	private String post_tags = "";
	@ExtractBy("//*[@id='article_edu_void']/div[1]/div[2]/div/div[4]/a/allText()")
	private List<String> tags;
	@ExtractBy("//*div[@class='showContent']/allText()")
	private String post_content = "";
	private String extractTime = "";
	@ExtractBy("//*[@id='article_edu_void']/div[1]/div[2]/div/div[3]/span/text()")
	private String post_time;
	private String pageMD5 = "";
	private String post_urlMD5 = "";
	@ExtractBy("//*[@class='showType']/span[1]/text()")
	private String view_num = "";
	@ExtractBy("//*[@class='showType']/span[2]/text()")
	private String reply_num = "";
	@ExtractBy("//*[@id='favourdiv']/text() ")
	private String vote_ups="";
	private int vote_up = 0;
	@ExtractBy("/html/body/div[7]/div[1]/div[1]/div[1]/h2/strong/text()")
	private String author_name = "";
	@ExtractBy("/html/body/div[3]/div/div[1]/div[2]/a[1]/@href")
	private String author_url = "";
	private int history = 0;
	private String type="blog";
	@ExtractBy("//*[@id='article_edu_void']/div[1]/div[2]/div/div[8]/div[1]/a[1]/allText()")
	private String category;
	private String source="51cto blog";
	public void afterProcess(Page page) {
		// 处理帖子的url
		this.post_url = page.getPageUrl();
		// 处理帖子的ID
		this.post_id = (Integer.parseInt(post_url.substring(post_url
				.lastIndexOf("/") + 1)));
		/*//处理tags
		this.tags=tags.subList(0,tags.size()-1);*/
		/*System.out.println("Tags 处理后："+tags);*/
		// 处理帖子的标签
		this.post_tags = StringHandler.combineTags(tags);
		/*System.out.println("post_tags 结果为："+post_tags);*/
		// 处理帖子的UrlMD5
		this.post_urlMD5 = DigestUtils.md5Hex(this.post_url);

		/*
		 * // 处理帖子的回复次数 this.view_num = StringHandler.subString(this.view_num,
		 * "回复次数："); //处理帖子的评论次数
		 * this.reply_num=StringHandler.subString(reply_num, null);
		 */

		/*
		 * // 处理帖子的内容 for (String str : this.tags) this.post_content =
		 * StringHandler.subString(this.post_content, str.trim());
		 * this.post_content = StringHandler.subString(this.post_content,
		 * "更多 分享到：");
		 */

		// 处理帖子的提交时间
		this.post_time = DateHandler.formatAllTypeDate(this.post_time,
				page.getTime());

		// 处理extractorTime
		this.extractTime = DateHandler.getExtractTime();

		// 处理pageMD5
		this.pageMD5 = DigestUtils.md5Hex(this.reply_num + this.view_num
				+ this.vote_up);
		//处理点赞数
		this.vote_up=StringHandler.extractIntFromString(this.vote_ups);

	}

	public void validate(Page page) {

		if (StringHandler.isAtLeastOneBlank(this.post_url, this.post_title,
				this.extractTime)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!StringHandler.canFormatterInteger(this.reply_num, this.view_num)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!DateHandler.canFormatToDate(this.post_time, this.extractTime)) {
			page.setResultSkip(this, true);
			return;
		}

	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public int getPost_id() {
		return post_id;
	}

	public void setPost_id(int post_id) {
		this.post_id = post_id;
	}

	public String getPost_url() {
		return post_url;
	}

	public void setPost_url(String post_url) {
		this.post_url = post_url;
	}

	public String getPost_title() {
		return post_title;
	}

	public void setPost_title(String post_title) {
		this.post_title = post_title;
	}

	public String getPost_tags() {
		return post_tags;
	}

	public void setPost_tags(String post_tags) {
		this.post_tags = post_tags;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public String getPost_content() {
		return post_content;
	}

	public void setPost_content(String post_content) {
		this.post_content = post_content;
	}

	public String getextractTime() {
		return extractTime;
	}

	public void setextractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getPost_time() {
		return post_time;
	}

	public void setPost_time(String post_time) {
		this.post_time = post_time;
	}

	public String getPageMD5() {
		return pageMD5;
	}

	public void setPageMD5(String pageMD5) {
		this.pageMD5 = pageMD5;
	}

	public String getPost_urlMD5() {
		return post_urlMD5;
	}

	public void setPost_urlMD5(String post_urlMD5) {
		this.post_urlMD5 = post_urlMD5;
	}

	public String getView_num() {
		return view_num;
	}

	public void setView_num(String view_num) {
		this.view_num = view_num;
	}

	public String getReply_num() {
		return reply_num;
	}

	public void setReply_num(String reply_num) {
		this.reply_num = reply_num;
	}

	public String getVote_ups() {
		return vote_ups;
	}

	public void setVote_ups(String vote_ups) {
		this.vote_ups = vote_ups;
	}

	public int getVote_up() {
		return vote_up;
	}

	public void setVote_up(int vote_up) {
		this.vote_up = vote_up;
	}

	public String getAuthor_name() {
		return author_name;
	}

	public void setAuthor_name(String author_name) {
		this.author_name = author_name;
	}

	public String getAuthor_url() {
		return author_url;
	}

	public void setAuthor_url(String author_url) {
		this.author_url = author_url;
	}

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}

}
